import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:tree_hole/Tabs.dart';
import 'RegisterPage.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;

class LoginPage extends StatefulWidget {
  @override
  _LoginPage createState() => new _LoginPage();
}

class _LoginPage extends State<LoginPage> {

  String username;
  String password;
  var context;
  String data;

  _login() async {
    var url = "http://121.196.101.7:8080/treehole/user/login";
    int code ;
      var response1 = await http.post(url,body: {"username":username,"password":password});
      var response2=json.decode(response1.body);
      code = response2["code"];
      data=response2["data"];
      if(code==0){
        SharedPreferences sharedPreferences =await SharedPreferences.getInstance();
        sharedPreferences.setString('username', username);
        sharedPreferences.setString('password', password);
        _toast3();
        Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context){
          return Tabs(this.data);},));
      }else{_toast4();}
  }


  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      resizeToAvoidBottomPadding: false,
      appBar: new AppBar(
        title: new Text('登录',style:TextStyle(color: Colors.black)),
        backgroundColor: Colors.white70,
      ),
      body: Container(
        padding: EdgeInsets.all(20),
        decoration: BoxDecoration(
          image: new DecorationImage(
            fit: BoxFit.cover,
            image: new AssetImage(
                'images/paper6.jpg'),
          ),
        ),
        child: Column(
          children: [
            SizedBox(height: 100),
            TextField(
              decoration: InputDecoration(
                icon: Icon(Icons.account_circle_outlined),
                hintText: "请输入用户名",
                filled: true,
                fillColor: Colors.transparent,
                enabledBorder: OutlineInputBorder(
                  borderRadius: BorderRadius.all(
                    Radius.circular(15),
                  ),
                  borderSide: BorderSide(
                    color: Colors.white,
                    width: 1,
                  ),
                ),
                contentPadding:EdgeInsets.all(10),
                isCollapsed: true,
              ),
              onChanged: (value){
                this.username=value;
              },

            ),
            SizedBox(height: 20),
            TextField(
              decoration: InputDecoration(
                  icon: Icon(Icons.lock_outline_rounded),
                  hintText: "请输入密码",
                  filled: true,
                  fillColor: Colors.transparent,
                  enabledBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.all(
                      Radius.circular(15),
                    ),
                    borderSide: BorderSide(
                      color: Colors.white,
                      width: 1,
                    ),
                  ),
                contentPadding:EdgeInsets.all(10),
                isCollapsed: true,
              ),
              obscureText: true,
              onChanged: (value){
                this.password=value;
              },
            ),
            SizedBox(height: 20,),
            Container(
              height: 80,
              child: Row(
                children: [
                  Expanded(
                      flex: 1,
                      child: Container(
                          padding:EdgeInsets.only(left: 20),
                          width: 80,
                          child: RaisedButton(
                            child: Text("登录"),
                            color: Colors.greenAccent[100],
                            elevation: 25,
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(10)
                            ),
                            onPressed: () {
                              if(this.username==null){
                                _toast1();
                              }else if(this.password==null){
                                _toast2();
                              }else{
                                this.context=context;
                                _login();
                                // Navigator.of(context).push(MaterialPageRoute(builder: (context){
                                //   return Tabs();},));
                              }
                            },
                          )
                      )
                  ),

                  Expanded(
                      flex: 1,
                      child: Container(
                          padding:EdgeInsets.only(left: 20),
                          width: 80,
                          child: RaisedButton(
                            child: Text("注册"),
                            color: Colors.greenAccent[100],
                            elevation: 25,
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(10)
                            ),
                            onPressed: () {
                              Navigator.push(context, MaterialPageRoute(builder: (context) {
                                return RegisterPage();
                              }));
                            },
                          )
                      )
                  )
                ],
              ),
            )
          ],
        ),
      ),

    );

  }
}


_toast1(){
  Fluttertoast.showToast(
    msg: "用户名不能为空",
    backgroundColor: Colors.amber[50],
    textColor: Colors.black,
    fontSize: 15,
    toastLength: Toast.LENGTH_SHORT,
    gravity: ToastGravity.BOTTOM,
  );
}

_toast2(){
  Fluttertoast.showToast(
    msg: "密码不能为空",
    backgroundColor: Colors.amber[50],
    textColor: Colors.black,
    fontSize: 15,
    toastLength: Toast.LENGTH_SHORT,
    gravity: ToastGravity.BOTTOM,
  );
}

_toast3(){
  Fluttertoast.showToast(
    msg: "登录成功",
    backgroundColor: Colors.amber[50],
    textColor: Colors.black,
    fontSize: 15,
    toastLength: Toast.LENGTH_SHORT,
    gravity: ToastGravity.BOTTOM,
  );
}

_toast4(){
  Fluttertoast.showToast(
    msg: "用户名或密码错误",
    backgroundColor: Colors.amber[50],
    textColor: Colors.black,
    fontSize: 15,
    toastLength: Toast.LENGTH_SHORT,
    gravity: ToastGravity.BOTTOM,
  );
}






